﻿using AutoRegisterAndActive;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace GenerateActiveCode
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string machineCode = Console.ReadLine();
            string activeCode = CreateRegisterCode(machineCode,DateTime.Now);
            Console.WriteLine(activeCode);
            Console.ReadKey();
        }


        static string CreateRegisterCode(string machineCode, DateTime overTime)
        {
            string machineCodeEncryptKey = "1996";
            //格式：机器码&过期时间&注册时间
            var finalCode = machineCode + "&" + overTime.ToString("s") + "&" + DateTime.Now.ToString("s");
            //加密
            return ToEncryptString(machineCodeEncryptKey, finalCode);
        }


        static string ToEncryptString(string key, string str)
        {
            try
            {
                //将密钥字符串转换为字节序列
                var P_byte_key = Encoding.Unicode.GetBytes(key);
                //将字符串转换为字节序列
                var P_byte_data = Encoding.Unicode.GetBytes(str);
                //创建内存流对象
                MemoryStream mStream = new MemoryStream();
                {
                    using (CryptoStream P_CryptStream_Stream = new CryptoStream(mStream, new DESCryptoServiceProvider().CreateEncryptor(P_byte_key, P_byte_key), CryptoStreamMode.Write))
                    {
                        //向加密流中写入字节序列
                        P_CryptStream_Stream.Write(P_byte_data, 0, P_byte_data.Length);
                        //将数据压入基础流
                        P_CryptStream_Stream.FlushFinalBlock();
                        //从内存流中获取字节序列
                        var res = mStream.ToArray();
                        P_CryptStream_Stream.Dispose();
                        mStream.Dispose();
                        return Convert.ToBase64String(res);
                    }
                }
            }
            catch (CryptographicException ce)
            {
                throw new Exception(ce.Message);
            }
        }
    }



}
